const conn = require('../app/database')

class UserService {
  async create(user) {
    const {name, password} = user
    // console.log('user: ---', user);
    const statement = `INSERT INTO user (name, password) VALUES (?,?);`

    const result = await conn.execute(statement, [name, password])

    // result: [rows, fields]
    return result[0]
  }

  async getUserByName(name) {
    const statement = `SELECT * FROM user WHERE name = ?`
    const result = await conn.execute(statement, [name])
    // result: [rows, fields]
    // console.log('result[0]: ---', result[0]);
    return result[0]
  }

  async updateAvatarUrlByUserId(avatarUrl, userId) {
    const statement = `UPDATE user SET avatar_url = ? WHERE id = ? ;`
    const result = await conn.execute(statement, [avatarUrl, userId])
    return result[0]
  }

}

module.exports = new UserService()